/**
 * 转换后台指定日期的 年月日时分秒格式
 * @param fmt
 * @param date
 * @returns {string|number}
 */
function dateFormatter(fmt, date) {
    let dateTime = new Date(date)
    let year = dateTime.getFullYear()
    let month = function () {
        return dateTime.getMonth() < 9 ? '0' + (dateTime.getMonth() + 1) : dateTime.getMonth() + 1
    }
    let day = function () {
        return dateTime.getDate() < 10 ? '0' + dateTime.getDate() : dateTime.getDate()
    }
    let hour = function () {
        return dateTime.getHours() < 10 ? '0' + dateTime.getHours() : dateTime.getHours()
    }
    let minute = function () {
        return dateTime.getMinutes() < 10 ? '0' + dateTime.getMinutes() : dateTime.getMinutes()
    }
    let second = function () {
        return dateTime.getSeconds() < 10 ? '0' + dateTime.getSeconds() : dateTime.getSeconds()
    }
    //获取 年月日
    if (fmt == 'yyyy-MM-dd') return '' + year + '-' + month() + '-' + day()
    //获取年月
    if (fmt == 'yyyy-MM') return ' ' + year + '-' + month()
    //获取年
    if (fmt == 'yyyy') return ' ' + year
    //获取月
    if (fmt == 'MM') return ' ' + month
    //获取日
    if (fmt == 'dd') return ' ' + day()
    //获取昨天
    if (fmt == 'yesterday') return ' ' + day() - 1
    //获取时分秒
    if (fmt == 'hh-mm-ss') return ' ' + hour() + ':' + minute() + ':' + second()
    //获取时分
    if (fmt == 'hh-mm') return ' ' + hour() + ':' + minute()
    //获取分秒
    if (fmt == 'mm-ss') return minute() + ':' + second()
    // 获取时
    if (fmt == 'hh') return dateTime.getHours()
    //获取分
    if (fmt == 'mm') return minute()
    //获取秒
    if (fmt == 'ss') return second()
    //默认时分秒年月日
    return year + '-' + month() + '-' + day() + ' ' + hour() + ':' + minute() + ':' + second()
}


// 根据给定的时间转成时分秒格式
// 给定的是120分钟，转成时分秒
function timeFn(time) {
    var theTime = parseInt(time);// 秒
    var theTime1 = 0;// 分
    var theTime2 = 0;// 小时
    if (theTime > 60) {
        theTime1 = parseInt(theTime / 60);
        theTime = parseInt(theTime % 60);
        if (theTime1 > 60) {
            theTime2 = parseInt(theTime1 / 60);
            theTime1 = parseInt(theTime1 % 60);
        }
    }
    var result = "" + parseInt(theTime) + "秒";
    if (theTime1 > 0) {
        result = "" + parseInt(theTime1) + "分" + result;
    }
    if (theTime2 > 0) {
        result = "" + parseInt(theTime2) + "小时" + result;
    }
    return result;
}


/**
 * JS将毫秒数转成 x天 x小时 x分钟 x秒
 * @param time
 */
function parseTimeToDayHourMinutesSeconds(time) {
    let daysStr;
    let hoursStr;
    let minutesStr;
    let secondsStr;
    //天
    let day = (time / 1000) / (24 * 3600);
    if (day < 10) {
        daysStr = "0" + parseInt(day);
    } else {
        daysStr = parseInt(day) + "";
    }
    //时
    let hour = ((time / 1000) % (24 * 3600)) / 3600;
    if (hour < 10) {
        hoursStr = "0" + parseInt(hour);
    } else {
        hoursStr = parseInt(hour) + "";
    }
    //分
    let minute = ((time / 1000) % 3600) / 60;
    if (minute < 10) {
        minutesStr = "0" + parseInt(minute);
    } else {
        minutesStr = parseInt(minute) + "";
    }
    //秒
    let second = (time / 1000) % 60;
    if (second < 10) {
        secondsStr = "0" + parseInt(second);
    } else {
        secondsStr = parseInt(second) + "";
    }
    return daysStr + '天' + hoursStr + '小时' + minutesStr + '分钟' + secondsStr + '秒'
}

/**
 * 生成UUID
 * @returns {string}
 */
function guid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
    var uuid = s.join("");
    return uuid;
}


/**
 * 全局处理拦截器针对ajax的响应
 */
$(function () {
    $.ajaxSetup({
        beforeSend: function () {
        },
        complete: function (xhr) {
            if (xhr.responseText === 'isAjax') {
                alert('您的账号已在其他设备登录')
                window.location.href = '/'
            }
        },
        success: function (response) {
        }
    })
})